#import "myJobViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "CouponListGingViewController.h"
#import "CouponGetListViewController.h"
#import "CouponListNewViewController.h"
#import "addCouponViewController.h"
@interface myJobViewController ()<UINavigationControllerDelegate>

- (void)reloadChildViewControllers;
- (void)enableScrollsToTop;
- (void)disableScrollsToTop;

@end

@implementation myJobViewController
{   UIButton* bu;
    UIButton* bu1;
    UIButton* bu2;
    NSArray *normalImageNames1;
    NSArray *normalImageNames2;
    NSArray *normalImageNames;
    
}
@synthesize viewControllers = _viewControllers;
@synthesize scrollView = _scrollView;
@synthesize pageControl = _pageControl;
@synthesize titleLabel = _titleLabel;

#pragma mark - life cycle

- (id)init
{
    self = [super init];
    if (self) {
        [self view];
        [self addObserver:self
               forKeyPath:@"viewControllers"
                  options:NSKeyValueObservingOptionNew
                  context:nil];
    }
    return self;
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO
    ;
       UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
}

- (void)loadView
{
    [super loadView];
   
    UIButton*sxBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
    [sxBtn setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
    [sxBtn addTarget:self action:@selector(addProduct:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:sxBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;

    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.scrollsToTop = NO;
    self.scrollView.delegate = self;
    self.scrollView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
    self.scrollView.frame = CGRectMake(0, 50, ScreenWidth, ScreenHeight-50);
    self.scrollView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.scrollView];
    
    CALayer *topShadowLayer = [CALayer layer];
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(-10, -10, 10000, 13)];
    topShadowLayer.frame = CGRectMake(-ScreenWidth, 0, 10000, 20);
    topShadowLayer.masksToBounds = YES;
    topShadowLayer.shadowOffset = CGSizeMake(2.5, 2.5);
    topShadowLayer.shadowOpacity = 0.5;
    topShadowLayer.shadowPath = [path CGPath];
    [self.scrollView.layer addSublayer:topShadowLayer];
    
    CALayer *bottomShadowLayer = [CALayer layer];
    path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 10000, 13)];
    bottomShadowLayer.frame = CGRectMake(-ScreenWidth, ScreenHeight-58, 10000, 20);
    bottomShadowLayer.masksToBounds = YES;
    bottomShadowLayer.shadowOffset = CGSizeMake(-2.5, -2.5);
    bottomShadowLayer.shadowOpacity = 0.5;
    bottomShadowLayer.shadowPath = [path CGPath];
    [self.scrollView.layer addSublayer:bottomShadowLayer];
    
    UIView *titleView = [[UIView alloc] init] ;
    titleView.frame = CGRectMake(0, 0, 150, 44);
    
    
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.frame = CGRectMake(0, 10, 150, 20);
    self.titleLabel.font = [UIFont systemFontOfSize:tetlefot];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor blackColor];
    [titleView addSubview:self.titleLabel];
    
    self.navigationItem.titleView = titleView;
    
    
    UIView* vi=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    vi.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:vi];
    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0, 49.5, ScreenWidth, 0.5)];
    label.backgroundColor=labelColor;
    [vi addSubview:label];
    
    self.pageControl = [[UIPageControl alloc] init] ;
    self.pageControl.frame = CGRectMake(0, 27, 150, 14);
    [self reloadViewControllers];
    [self initCustomTabBar];
    self.pageControl.numberOfPages = 3;
    self.pageControl.currentPage = 0;
   
}
- (void)reloadViewControllers
{
    
    
    CouponListNewViewController *viewController1 = [[CouponListNewViewController alloc] init] ;
    viewController1.navigationItem.title = @"门店优惠券";
    
    CouponListGingViewController *viewController2 = [[CouponListGingViewController alloc] init] ;
    viewController2.navigationItem.title = @"门店优惠券";
    
    CouponGetListViewController *viewController3 = [[CouponGetListViewController alloc] init];
    viewController3.navigationItem.title = @"门店优惠券";
    
    self.viewControllers = [NSArray arrayWithObjects:
                            viewController1,
                            viewController2,
                            viewController3, nil];
    
}

- (void)initCustomTabBar {
    
    
    self.customTabBar = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 50)];
    //添加移动的图片
    self.moveImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tabbar_slider.png"]];
    self.moveImage.backgroundColor=luseColor;
    self.moveImage.frame = CGRectMake(0, 0, ScreenWidth/3, 1.5);
    CGFloat sliderImageViewCenterX = ScreenWidth/3/2;
    [self.moveImage setCenter:CGPointMake(sliderImageViewCenterX, 49)];
    [self.customTabBar addSubview:_moveImage];
    [self.view addSubview:self.customTabBar];
    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0, 49.5, ScreenWidth, 0.5)];
    label.backgroundColor=labelColor;
    [self.customTabBar addSubview:label];
    
    bu=[[UIButton alloc]initWithFrame:CGRectMake(0, 0,ScreenWidth/3, 50)];
    [bu setTitle:@"未开始" forState:UIControlStateNormal];
    [bu setTitleColor:titleColor forState:UIControlStateNormal];
    bu .titleLabel.font=[UIFont systemFontOfSize:16];
    [bu addTarget:self action:@selector(tabBarItemClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.customTabBar addSubview:bu];
    
    bu1=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3, 0, ScreenWidth/3, 50)];
    [bu1 setTitle:@"进行中" forState:UIControlStateNormal];
    [bu1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    bu1 .titleLabel.font=[UIFont systemFontOfSize:16];
    [bu1 addTarget:self action:@selector(tabBarItemClicked1) forControlEvents:UIControlEventTouchUpInside];
    [self.customTabBar addSubview:bu1];
    
    bu2=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3*2, 0, ScreenWidth/3, 50)];
    [bu2 setTitle:@"已过期" forState:UIControlStateNormal];
    [bu2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    bu2 .titleLabel.font=[UIFont systemFontOfSize:16];
    [bu2 addTarget:self action:@selector(tabBarItemClicked2) forControlEvents:UIControlEventTouchUpInside];
    [self.customTabBar addSubview:bu2];
    

    
}


-(void)tabBarItemClicked{
    //更换moveImage的位置
    [bu2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [bu1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [bu setTitleColor:titleColor forState:UIControlStateNormal];
    CGFloat sliderImageViewCenterX = ScreenWidth/3/2 + (0 * ScreenWidth/3);
    [UIView animateWithDuration:0.2f animations:^{
        self.moveImage.center = CGPointMake(sliderImageViewCenterX, self.moveImage.center.y);
        
        
        
    }];
  
    [self.scrollView setContentOffset:CGPointMake(0, -64) ];
   self.scrollView.bouncesZoom=NO;
}





-(void)tabBarItemClicked1{
    //更换moveImage的位置
    [bu2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    [bu1 setTitleColor:titleColor forState:UIControlStateNormal];
    [bu setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    CGFloat sliderImageViewCenterX = ScreenWidth/3/2 + (1 * ScreenWidth/3);
    [UIView animateWithDuration:0.2f animations:^{
        self.moveImage.center = CGPointMake(sliderImageViewCenterX, self.moveImage.center.y);
        
        
        
    }];
 
    [self.scrollView setContentOffset:CGPointMake(ScreenWidth,-64) ];
   self.scrollView.bouncesZoom=NO;
}


-(void)tabBarItemClicked2{
    //更换moveImage的位置
    
    [bu1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [bu2 setTitleColor:titleColor forState:UIControlStateNormal];
    [bu setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    CGFloat sliderImageViewCenterX = ScreenWidth/3/2 + (2 * ScreenWidth/3);
    [UIView animateWithDuration:0.2f animations:^{
        self.moveImage.center = CGPointMake(sliderImageViewCenterX, self.moveImage.center.y);
        
        
        
    }];
    
    [self.scrollView setContentOffset:CGPointMake(ScreenWidth*2, -64) ];
  self.scrollView.bouncesZoom=NO;
}





- (void)viewDidLoad
{
    [super viewDidLoad];
    [self reloadChildViewControllers];
}

- (void)viewDidUnload
{
    self.scrollView = nil;
    
    [super viewDidUnload];
}
- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"viewControllers"];
//       [super dealloc];
}

#pragma mark - key value observation

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == self && [keyPath isEqualToString:@"viewControllers"]) {
        [self reloadChildViewControllers];
        [self disableScrollsToTop];
    }
}

#pragma mark - action

- (void)reloadChildViewControllers
{
    
    
    
    
    for (UIViewController *viewController in self.childViewControllers) {
        [viewController willMoveToParentViewController:nil];
        [viewController removeFromParentViewController];
        [viewController.view removeFromSuperview];
    }
    for (UIViewController *viewController in self.viewControllers) {
        NSInteger index = [self.viewControllers indexOfObject:viewController];
        viewController.view.frame = CGRectMake(self.scrollView.frame.size.width * index,
                                               0,
                                               self.scrollView.frame.size.width,
                                               self.scrollView.frame.size.height);
        
        [self addChildViewController:viewController];
        [self.scrollView addSubview:viewController.view];
        [viewController didMoveToParentViewController:self];
        if (index == self.pageControl.currentPage) {
            self.titleLabel.text = viewController.navigationItem.title;
        }
    }
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [self.viewControllers count], 1);
    
    for (UIViewController *viewController in self.childViewControllers) {
        CALayer *layer = viewController.view.layer;
        layer.shadowOpacity = .5f;
        layer.shadowOffset = CGSizeMake(10, 10);
        layer.shadowPath = [UIBezierPath bezierPathWithRect:viewController.view.bounds].CGPath;
    }
}

- (void)enableScrollsToTop
{
    // FIXME: this code affects all scroll view
    for (UIViewController *viewController in self.viewControllers) {
        for (UIView *subview in [viewController.view subviews]) {
            if ([subview respondsToSelector:@selector(scrollsToTop)]) {
                [(UIScrollView *)subview setScrollsToTop:YES];
                
            }
        }
    }
}

- (void)disableScrollsToTop
{
    for (UIViewController *viewController in self.viewControllers) {
        NSInteger index = [self.viewControllers indexOfObject:viewController];
        if (index != self.pageControl.currentPage) {
            for (UIView *subview in [viewController.view subviews]) {
                if ([subview respondsToSelector:@selector(scrollsToTop)]) {
                    [(UIScrollView *)subview setScrollsToTop:NO];
                }
            }
        }
    }
}

#pragma mark - scroll view delegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //    [self enableScrollsToTop];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //    [self disableScrollsToTop];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offset = scrollView.contentOffset.x;
    CGFloat width = scrollView.frame.size.width;
    NSInteger currentPage = (offset+(width/2))/width;
    if (currentPage != self.pageControl.currentPage && currentPage < [self.viewControllers count]) {
        UIViewController <myJobViewControllerChild> *previousViewController = [self.viewControllers objectAtIndex:self.pageControl.currentPage];
        if ([previousViewController respondsToSelector:@selector(didResignActive)]) {
            [previousViewController didResignActive];
        }
        
        UIViewController <myJobViewControllerChild> *currentViewController = [self.viewControllers objectAtIndex:currentPage];
        if ([currentViewController respondsToSelector:@selector(didBecomeActive)]) {
            [currentViewController didBecomeActive];
        }
        self.titleLabel.text = currentViewController.navigationItem.title;
        self.pageControl.currentPage = currentPage;
        CGFloat sliderImageViewCenterX = ScreenWidth/3/2 + (currentPage * ScreenWidth/3);
        
       
        
        [UIView animateWithDuration:0.2f animations:^{
            self.moveImage.center = CGPointMake(sliderImageViewCenterX, self.moveImage.center.y);
            
        }];
        
        
    }
   
    
    
    
    
    
    
    if (self.scrollView.bounds.origin.x==0) {
        
        [bu1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [bu setTitleColor:titleColor forState:UIControlStateNormal];
        [bu2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

    }
    if (self.scrollView.bounds.origin.x==ScreenWidth) {
        [bu1 setTitleColor:titleColor forState:UIControlStateNormal];
        [bu setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [bu2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

       
    }
    if (self.scrollView.bounds.origin.x==ScreenWidth*2) {
        [bu2 setTitleColor:titleColor forState:UIControlStateNormal];
        [bu setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [bu1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];

       
    }

//    for (UIViewController *viewController in self.viewControllers) {
//        NSInteger index = [self.viewControllers indexOfObject:viewController];
//        CGFloat width = self.scrollView.frame.size.width;
//        CGFloat y = index * width;
//        CGFloat value = (offset-y)/width;
//        CGFloat scale = 1.f-fabs(value);
//        if (scale > 1.f) scale = 1.f;
//        if (scale < .8f) scale = .8f;
//        
//        viewController.view.transform = CGAffineTransformMakeScale(scale, scale);
//    }
//    
//    for (UIViewController *viewController in self.childViewControllers) {
//        CALayer *layer = viewController.view.layer;
//        layer.shadowPath = [UIBezierPath bezierPathWithRect:viewController.view.bounds].CGPath;
//    }
}
-(void)addProduct:(UIButton*)sender{

    
    addCouponViewController *detailViewController = [[addCouponViewController alloc] init];
    [self.navigationController pushViewController:detailViewController animated:YES];
}

@end
